<template>
    <section class="chart-container">
        <el-row>
            <el-col :span="12">
                <div id="chartColumn" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartBar" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartLine" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartPie" style="width:100%; height:400px;"></div>
            </el-col>
        </el-row>
    </section>
</template>

<script>
import echarts from 'echarts'

export default {
    data() {
        return {
            chartColumn: null,
            chartBar: null,
            chartLine: null,
            chartPie: null,
            articles: [], // 文章数据
            articleTypes: [], // 文章类型数据
        }
    },

    methods: {
        drawColumnChart() {
            // 获取文章点赞前6的文章数据
            this.$http.get("/article/articleRanking").then(res => {
                // 给文章数据赋值
                this.articles = res.data;
                // 遍历文章数据, 获取文章名字
                var articleNames = this.articles.map(obj => obj.articleName);
                console.log(articleNames)
                // 遍历文章数据, 获取文章点赞数
                var articleStarNums = this.articles.map(obj => obj.articleStarNum);
                this.chartColumn = echarts.init(document.getElementById('chartColumn'));
                this.chartColumn.setOption({
                    title: { text: '文章点赞排名' },
                    tooltip: {},
                    xAxis: [{
                        data: articleNames,
                        axisLabel: {
                            interval: 0, //控制X轴刻度全部显示
                            rotate: 30 //倾斜角度
                        }
                    }],
                    yAxis: {},
                    series: [{
                        name: '点赞量',
                        type: 'bar',
                        data: articleStarNums
                    }]
                });
        })
    },
    drawBarChart() {
        this.chartBar = echarts.init(document.getElementById('chartBar'));
        this.chartBar.setOption({
            title: {
                text: 'Bar Chart',
                subtext: '数据来自网络'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: ['2011年', '2012年']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'value',
                boundaryGap: [0, 0.01]
            },
            yAxis: {
                type: 'category',
                data: ['巴西', '印尼', '美国', '印度', '中国', '世界人口(万)']
            },
            series: [
                {
                    name: '2011年',
                    type: 'bar',
                    data: [18203, 23489, 29034, 104970, 131744, 630230]
                },
                {
                    name: '2012年',
                    type: 'bar',
                    data: [19325, 23438, 31000, 121594, 134141, 681807]
                }
            ]
        });
    },
    drawLineChart() {
        this.chartLine = echarts.init(document.getElementById('chartLine'));
        this.chartLine.setOption({
            title: {
                text: 'Line Chart'
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['邮件营销', '联盟广告', '搜索引擎']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name: '邮件营销',
                    type: 'line',
                    stack: '总量',
                    data: [120, 132, 101, 134, 90, 230, 210]
                },
                {
                    name: '联盟广告',
                    type: 'line',
                    stack: '总量',
                    data: [220, 182, 191, 234, 290, 330, 310]
                },
                {
                    name: '搜索引擎',
                    type: 'line',
                    stack: '总量',
                    data: [820, 932, 901, 934, 1290, 1330, 1320]
                }
            ]
        });
    },
    drawPieChart() {
        // 获取文章类型的文章数量最多的前5个
        this.$http.get("article/articleTypeRanking").then(res => {
            // 给文章类型数据赋值
            this.articleTypes = res.data;
            // 获取文章类型名称
            var articleTypeNames = this.articleTypes.map(obj => obj.articleTypeName);
            // 获取文章类型的文章数量与文章名称的对象数组
            var articleTypes = []
            this.articleTypes.forEach(obj => {
                articleTypes.push({
                    value: obj.articleTypeNum,
                    name: obj.articleTypeName
                })
            })
            this.chartPie = echarts.init(document.getElementById('chartPie'));
            this.chartPie.setOption({
                title: {
                    text: '文章类型',
                    subtext: '纯属虚构',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: articleTypeNames
                },
                series: [
                    {
                        name: '访问来源',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '60%'],
                        data: articleTypes,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            });
        })
    },
    drawCharts() {
        this.drawColumnChart()
        this.drawBarChart()
        this.drawLineChart()
        this.drawPieChart()
    },
},

mounted: function () {
    this.drawCharts();

},
updated: function () {
    this.drawCharts()
}
}
</script>

<style scoped>
.chart-container {
    width: 100%;
    float: left;
}

/*.chart div {
        height: 400px;
        float: left;
    }*/

.el-col {
    padding: 30px 20px;
}
</style>
